package entity

import (
	"github.com/golang/protobuf/proto"
	"kop/pb"
	"time"
)

type Boss struct {
	GuildID    int32 `gorm:"type:int;primary_key"`
	BossID     int32 `gorm:"type:int;primary_key"`
	HP         int64
	Open       bool
	AwardsData []byte // proto.Marshal(self.Awards)
	CreatedAt  time.Time
	UpdatedAt  time.Time
	//CaptainsJson []byte `gorm:"type:jsonb"` // json.Marshal(self.CaptainIDs)
	//CaptainIDs map[int32][]int32    `sql:"-"`
	Awards []*pb.GuildBossAward `sql:"-"`
}

func (Boss) TableName() string {
	return "guild_bosses"
}

func (b *Boss) AfterFind() error {

	//b.CaptainIDs = make(map[int32][]int32)
	//_ = json.Unmarshal(b.CaptainsJson, &b.CaptainIDs)

	var message pb.GuildBossAwardResp
	_ = proto.Unmarshal(b.AwardsData, &message)
	if message.GuildBossAwards != nil {
		b.Awards = message.GuildBossAwards
	}
	return nil
}

func (b *Boss) BeforeSave() error {

	//b.CaptainsJson, _ = json.Marshal(b.CaptainIDs)
	b.AwardsData, _ = proto.Marshal(&pb.GuildBossAwardResp{GuildBossAwards: b.Awards})
	return nil
}

func (b *Boss) BeforeUpdate() error {

	//b.CaptainsJson, _ = json.Marshal(b.CaptainIDs)
	b.AwardsData, _ = proto.Marshal(&pb.GuildBossAwardResp{GuildBossAwards: b.Awards})
	return nil
}

func (b *Boss) GetHP() int64 {
	if b.HP < 0 {
		b.HP = 0
	}
	return b.HP
}

func (b *Boss) Reset() {
	b.HP = 0
	b.Open = false
	//b.CaptainIDs = make(map[int32][]int32)
}

// 初始化奖励领取情况
func (b *Boss) InitAwards(length int) {

	b.Awards = make([]*pb.GuildBossAward, length)
	for k := range b.Awards {
		b.Awards[k] = &pb.GuildBossAward{Award: &pb.Award{}}
	}
}
